import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0
  },
  // mutations中才可以修改state中的数据
  mutations: {
    add (state, step) {
      // 不要在 mutations 函数中执行异步操作
      // setTimeout( () => {
      //   state.count += step || 1
      // }, 1000)
      state.count += step || 1
    },
    sub (state, step) {
      state.count -= step || 1
    }
  },
  // 只负责执行异步操作
  actions: {
    addAsync (context, step) {
      setTimeout(() => {
        context.commit('add', step)
      }, 1000)
    },
    subAsync (context, step) {
      setTimeout(() => {
        context.commit('sub', step)
      }, 1000)
    }
  },
  getters: {
    showNum (state) {
      return '当前的最新数量是【' + state.count + '】'
    }
  },
  modules: {
  }
})
